package com.epam.mykyta.grusha.anotations.main;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import com.epam.mykyta.grusha.anotations.Init;
import com.epam.mykyta.grusha.anotations.Service;
import com.epam.mykyta.grusha.anotations.implementation.LazyService;
import com.epam.mykyta.grusha.anotations.implementation.SimpleService;

public class LetsTry {
	static Map<String, Object> servicesMap = new HashMap<>();

	public static void main(String[] args) throws Throwable {
		inspectService(SimpleService.class);
		inspectService(LazyService.class);
		inspectService(String.class);

		loadService("com.epam.mykyta.grusha.anotations.implementation.SimpleService");
		loadService("com.epam.mykyta.grusha.anotations.implementation.LazyService");
		loadService("java.lang.String");
		for (String str : servicesMap.keySet()) {
			System.out.println(str);
		}
		Class temp = Class
				.forName("com.epam.mykyta.grusha.anotations.main.LetsTry");
		System.out.println(temp);
	}

	static void inspectService(Class<?> service) {
		if (service.getMethods() != null) {
			for (Method method : service.getDeclaredMethods()) {
				if (method.isAnnotationPresent(Init.class)) {
					System.out.println("WE FOUND IT : " + method.getName());
				}

			}
		}
		if (service.isAnnotationPresent(Service.class)) {
			Service ann = service.getAnnotation(Service.class);
			System.out.println(ann.name());
		} else {
			System.out.println("Not found");
		}
	}

	static void loadService(String className) throws Throwable {
		Class<?> clazz = Class.forName(className);
		if (clazz.isAnnotationPresent(Service.class)) {
			Object obj = clazz.newInstance();
			servicesMap.put(clazz.getAnnotation(Service.class).name(), obj);
		}
	}
}
